package com.zh.work.service.platform.virtual.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.work.dao.platform.merch.MerchInfoMapper;
import com.zh.work.dao.platform.virtual.VirtualCardMapper;
import com.zh.work.entity.merch.MerchInfoEntity;
import com.zh.work.entity.virtual.VirtualCardEntity;
import com.zh.work.service.platform.virtual.VirtualCardService;
import com.zh.work.util.plateform.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 虚拟账户设置业务逻辑实现层
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/11
 * @Version: 1.0.1
 */
@Service
public class VirtualCardServiceImpl extends ServiceImpl<VirtualCardMapper, VirtualCardEntity> implements VirtualCardService {

    @Autowired
    private VirtualCardMapper mapper;

    @Autowired
    private MerchInfoMapper merchInfoMapper;


    @Override
    public IPage<VirtualCardEntity> selectVirtualCardPages(Page<VirtualCardEntity> page, VirtualCardEntity virtualCardEntity) {
        return this.mapper.selectVirtualCardPages(page, virtualCardEntity);
    }


    @Override
    public int putAddVirtualCard(VirtualCardEntity virtualCardEntity) {
        MerchInfoEntity merchInfoEntity = merchInfoMapper.selectOne(new LambdaQueryWrapper<MerchInfoEntity>().eq(MerchInfoEntity::getMerchNo, virtualCardEntity.getMerchNo()));
        if (merchInfoEntity == null) {
            return -1;
        } else {
            virtualCardEntity.setBranchNo(merchInfoEntity.getBranchNo());
            String cardNo = ServiceUtil.getCardNo(mapper.getMaxCardNo(), merchInfoEntity.getBranchNo());
            // 查询是否已经存在指定虚拟账号
            int i = mapper.selectCount(new LambdaQueryWrapper<VirtualCardEntity>().eq(VirtualCardEntity::getMerchNo, virtualCardEntity.getMerchNo()));
            if (i > 0) return -2;
            virtualCardEntity.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            virtualCardEntity.setCardNo(cardNo);
            // 添加
           return virtualCardEntity.insert()? 1 : 0;
        }
    }
}
